<template>
    <div class="">
        <el-dialog :title="isAdd ? '添加轮播图' : '编辑轮播图'" :visible.sync="isShow" :before-close="cancel" center>
            <el-form :model="bannerlist" ref="bannerlist" :rules="rules">
                <el-form-item label="标题" :label-width="formLabelWidth">
                    <el-input autocomplete="off" v-model="bannerlist.title"></el-input>
                </el-form-item>
                <el-form-item label="图片" :label-width="formLabelWidth">
                    <el-upload action="#" list-type="picture-card" :auto-upload="false" :on-preview="onPreview"
                        :on-remove="onRemove" :on-change="onChange" :file-list="fileList" :on-exceed="handleExceed"
                        :limit="1">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="" />
                    </el-dialog>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                        v-model="bannerlist.status" :inactive-value="2">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="confirm">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>
<script>
import { bannerAdd, bannerEdit, getbannerInfo } from '../../request/api'
import { mapGetters, mapActions } from 'vuex';
export default {
    props: ['isShow', 'isAdd'],
    data() {
        return {
            formLabelWidth: '120px',
            bannerlist: {
                // id: '',
                title: '',
                img: '',
                status: 1
            },
            dialogImageUrl: '',
            dialogVisible: false,
            fileList: [],
            rules: {
                title: [
                    { required: true, message: "请输入标题", trigger: "blur" },
                    { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
                ],

            },
        }
    },
    computed: {
        ...mapGetters({
            bannerList: 'banner/getBanner'
        })

    },
    mounted() {
        this.getBannerAction()
    },
    methods: {
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择1个文件,本次选择了${files.length}个文件,共选择了${files.length + fileList.length}个文件`)
        },
        // 图片变化
        onChange(files, fileList) {
            this.bannerlist.img = files.raw
        },
        // 图片预览
        onPreview(file) {
            this.dialogVisible = true
            this.dialogImageUrl = file.url
        },
        // 图片移除
        onRemove(file, fileList) {
            this.bannerlist.img = ''
        },
        cancel() {
            this.$emit('cancel', false)
            this.reset()
        },
        reset() {
            this.bannerlist = {
                // id: '',
                title: '',
                img: '',
                status: 1
            }
            this.$refs.bannerlist.resetFields();
            this.fileList = []
        },
        ...mapActions({
            getBannerAction: 'banner/getBannerAction'
        }),
        lookup(id) {
            getbannerInfo({ id }).then(res => {
                this.bannerlist = res.list
                this.bannerlist.id = id
                this.fileList = this.bannerlist.img ? [{ url: "http://localhost:3000" + this.bannerlist.img }] : []
            })
        },
        confirm() {
            console.log('111');
            this.$refs.bannerlist.validate((val) => {
                if (val) {
                    if (this.isAdd) {
                        bannerAdd(this.bannerlist).then((res) => {
                            if (res.code == 200) {
                                this.$message.success(res.msg)
                                this.cancel()
                                this.getBannerAction()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    } else {
                        bannerEdit(this.bannerlist).then(res => {
                            if (res.code == 200) {
                                this.$message.success(res.msg);
                                this.cancel()
                                this.getBannerAction()
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                    }
                }
            })
        }
    }
}
</script>
<style scoped lang="scss">

</style>